package com.chinasoft.bigdatadisplay.controller;


import com.chinasoft.bigdatadisplay.utils.SshExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/hive")
public class HiveSetupController {

    @Autowired
    private SshExecutor sshExecutor;

    /**
     * 执行远程服务器上的Hive表初始化脚本
     * @return 执行结果信息
     */
    @PostMapping("/setup-tables")
    public Map<String, Object> setupHiveTables() {
        Map<String, Object> result = new HashMap<>();
        String command = "/home/setup_hive_tables.sh"; // 注意脚本路径前是单斜杠，不是./
        
        try {
            // 调用SSH工具执行命令
            String executeResult = sshExecutor.executeCommand(command);
            
            // 处理成功结果
            result.put("success", true);
            result.put("message", "脚本执行成功");
            result.put("output", executeResult);
            log.info("Hive表初始化脚本执行成功: {}", executeResult);
        } catch (Exception e) {
            // 处理异常情况
            result.put("success", false);
            result.put("message", "脚本执行失败: " + e.getMessage());
            log.error("Hive表初始化脚本执行失败", e);
        }
        
        return result;
    }
}